#include "../include/head.h"

pInfoReq handle_query(char *query)
{
    pInfoReq result = (pInfoReq)malloc(sizeof(InfoReq));
    if (result == NULL) {
        return NULL; // 内存分配失败
    }
    // 初始化结构体
    result->userId = 0;
    strcpy(result->url, "");
    char buffer[1024];     // 用于存储临时字符串
    strcpy(buffer, query);
    char *pair = strtok(buffer, "^");
    while (pair != NULL)
    {
        if (pair != NULL)
        {
            result->userId = atoi(pair);
            pair = strtok(NULL, "^");
        }
        if (pair != NULL)
        {
            strncpy(result->url, pair, sizeof(result->url) - 1);
            result->url[sizeof(result->url) - 1] = '\0'; // 确保字符串以 '\0' 结尾
            pair = strtok(NULL, "^");
        }
    }
    return result;
}
#define MAX_TOKENS 100  // 假设最大分割的token数量不会超过100
#define MAX_TOKEN_LENGTH 100  // 假设每个token的长度不会超过100
// 下面的代码片段展示了如何正确地实现这个函数（使用动态分配）：
char** splitStringCorrect(const char* str, const char delim, int* arrayLength) {
    char tempStr[strlen(str) + 1];
    strcpy(tempStr, str);
 
    char *token = strtok(tempStr, &delim);
    int count = 0;
    while (token != NULL) {
        count++;
        token = strtok(NULL, &delim);
    }
 
    // 分配内存来存储token指针数组
    char** tokens = (char**)malloc(count * sizeof(char*));
    *arrayLength = count;
 
    strcpy(tempStr, str);
    token = strtok(tempStr, &delim);
    int index = 0;
    while (token != NULL) {
        tokens[index] = (char*)malloc((strlen(token) + 1) * sizeof(char));
        strcpy(tokens[index], token);
        index++;
        token = strtok(NULL, &delim);
    }
    return tokens;  // 返回动态分配的字符串指针数组的指针
}